﻿using NUnit.Framework;

namespace WpfApplication1.TaskModule
{
    [TestFixture]
    public class TasksViewModelTests
    {
        private TasksViewModel _viewModel;
        private TaskItemsRepositoryMock _repositoryMock;

        [SetUp]
        public void SetUp()
        {
            _repositoryMock = new TaskItemsRepositoryMock();
            _viewModel = new TasksViewModel(_repositoryMock.Object);     
        }

        [Test]
        public void PropertyChangedTest()
        {
            //arrange
            int counter = 0;
            _viewModel.PropertyChanged += (sender, e) => { if (e.PropertyName == "DisplayName") counter++; };            
            _viewModel.PropertyChanged += (sender, e) => { if (e.PropertyName == "SelectedItem") counter++; };
            
            //act
            _viewModel.SelectedItem = new TaskItem();
            _viewModel.DisplayName = "";

            //assert
            Assert.IsTrue(counter==2);
        }

        [Test]
        public void ReadCommandTest()
        {            
            //arrange

            //act
            _viewModel.ReadCommand.Execute(null);

            //assert
            Assert.IsTrue(_viewModel.Items.Count==2);
        }

        [Test]
        public void DeleteCommandTest()
        {
            //arrange
            bool flag = false;
            _repositoryMock.DeleteHandler += (sender,args) => flag = true;

            //act
            _viewModel.DeleteCommand.Execute(null);

            //assert
            Assert.IsTrue(flag);
        }

        [Test]
        public void SaveCommandTest()
        {
            //arrange
            bool flag = false;
            _repositoryMock.SaveHandler += (sender, args) => flag = true;

            //act
            _viewModel.UpdateCommand.Execute(null);

            //assert
            Assert.IsTrue(flag);
        }

        [Test]
        public void CanSaveTest()
        {
            //arrange
            //act
            bool retValue = _viewModel.UpdateCommand.CanExecute(null);

            //assert
            Assert.IsTrue(retValue);
        }
    }
}
